﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScytheEngine
{
	public abstract class GridElement
	{
		GridElement m_Up;
		GridElement m_Down;
		GridElement m_Left;
		GridElement m_Right;

		public GridElement Up 
		{
			get { return m_Up; }
			set { m_Up = value; }
		}
		public GridElement Down 
		{
			get { return m_Down; }
			set { m_Down = value; }
		}
		public GridElement Left 
		{
			get { return m_Left; }
			set { m_Left = value; }
		}
		public GridElement Right 
		{
			get { return m_Right; }
			set { m_Right = value; }
		}
	}

	public class Grid : ICollection<GridElement>
	{
		List<GridElement> m_GridElements = new List<GridElement>();

		#region Properties

		public GridElement this[GridElement index]
		{
			get { return m_GridElements[ m_GridElements.IndexOf( index ) ]; }
		}

		#endregion

		#region ICollection<IGridElement> Members

		public void Add( GridElement item )
		{
			m_GridElements.Add( item );
		}

		public void Clear()
		{
			m_GridElements.Clear();
		}

		public bool Contains( GridElement item )
		{
			return m_GridElements.Contains( item );
		}

		public void CopyTo( GridElement[] array, int arrayIndex )
		{
			throw new NotImplementedException();
		}

		public int Count
		{
			get { return m_GridElements.Count; }
		}

		public bool IsReadOnly
		{
			get { return false; }
		}

		public bool Remove( GridElement item )
		{
			if ( m_GridElements.Remove( item ) )
			{
				//fix up referencing grid elements
				if ( item.Up != null )
				{
					item.Up.Down = null;
				}
				if ( item.Down != null )
				{
					item.Down.Up = null;
				}
				if ( item.Left != null )
				{
					item.Left.Right = null;
				}
				if ( item.Right != null )
				{
					item.Right.Left = null;
				}
				return true;
			}
			return false;
		}

		#endregion

		#region IEnumerable<IGridElement> Members

		public IEnumerator<GridElement> GetEnumerator()
		{
			return m_GridElements.GetEnumerator();
		}

		#endregion

		#region IEnumerable Members

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}

		#endregion
	}
}
